package deng.qiya.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;

import deng.qiya.baidu.DistanceCompare;
import deng.qiya.model.BaiduLocation;
import deng.qiya.model.HotelDistanceModel;
import deng.qiya.model.HotelMapInfo;
import deng.qiya.util.CustomerContextHolder;
import deng.qiya.util.GeoUtil;
import deng.qiya.util.GeoUtil.GaussSphere;
import deng.qiya.wechat.Constant;

public class MapOper {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	//get all lat and lng information;
	@Cacheable("hotelsMap")
	public Map<String, HotelMapInfo> getAllHotelsLatLng(){
		
		Map<String,HotelMapInfo> map = new HashMap<String, HotelMapInfo>();
		String sqlString = "select hotelcd,hotelnm,tel,address,Google_Lon,Google_Lat from POIINFO";
		
		CustomerContextHolder.setCustomerType(Constant.dsmap);
		List<Map<String,Object>> rows = jdbcTemplate.queryForList(sqlString);
		
		for(Map<String, Object> row:rows){
			HotelMapInfo hmi = new HotelMapInfo();
			hmi.setHotelId((String)row.get("hotelcd"));
			hmi.setHotelName((String)row.get("hotelnm"));
			hmi.setTel((String)row.get("tel"));
			hmi.setAddress((String)row.get("address"));
			hmi.setLat((BigDecimal)row.get("Google_Lat"));
			hmi.setLng((BigDecimal)row.get("Google_Lon"));
			map.put(hmi.getHotelId(), hmi);
		}
		return map;
	}
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


	public List<HotelMapInfo> getNearHote(Map<String,HotelMapInfo> map,BaiduLocation location){
		
		List<HotelMapInfo> nearList = new ArrayList<HotelMapInfo>();
		List<HotelDistanceModel> distanceList = new ArrayList<HotelDistanceModel>();
		Collection<HotelMapInfo> colle = map.values();
		Iterator<HotelMapInfo> it = colle.iterator();
		while(it.hasNext()){
			HotelMapInfo mapInfo = it.next();
			if(mapInfo!=null && mapInfo.getLat()!=null && mapInfo.getLng()!=null) {
				double d = GeoUtil.DistanceOfTwoPoints(new Double(location.getLng()), new Double(location.getLat()), mapInfo.getLat().doubleValue(),mapInfo.getLng().doubleValue(),GaussSphere.Beijing54);
				distanceList.add(new HotelDistanceModel(mapInfo.getHotelId(), d));
			}
		}
		
		Collections.sort(distanceList, new DistanceCompare());
		
		//top 5 && distance <=2000
		for(int i=0; i<5; i++) {
			HotelDistanceModel hdm= distanceList.get(i);
			//if(hdm.getDistance() <= 2000) {
				HotelMapInfo info = map.get(hdm.getHotelId());
				info.setDistance(hdm.getDistance());
				nearList.add(info);
			//}
		}
		return nearList;
	}
	
	public HotelMapInfo getHotelMapInfoByHotelId(String hotelId){
		Map<String, HotelMapInfo> map = getAllHotelsLatLng();
		return map.get(hotelId);
	}
	
	public  String getHotelName(String hotelId) 
	{
		Map<String, HotelMapInfo> map = getAllHotelsLatLng();
		
		return map.get(hotelId).getHotelName();
		
	}
}
